home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / DirList.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  2.8 KB  |  143 lines

  1. #import "DirList.h"
  2. #import <libc.h>
  3.  
  4. static const char *const *extlist = NULL;
  5.  
  6. static const char *getExt(const char *p)
  7. {
  8.     int    i, j;
  9.     const char *xt = NULL;
  10.  
  11.     for (i = 0, j = -1; p[i]; i++)
  12.         if (p[i] == '.') j = i;
  13.     if (j <= 0) return NULL;
  14.     xt = &p[j + 1];
  15.     for (i = 0; extlist[i]; i++)
  16.         if (strcmp(extlist[i], xt) == 0) return xt;
  17.     return NULL;
  18. }
  19.  
  20. static int NXstrcmp(const unsigned char *a, const unsigned char *b)
  21. {
  22.     /* 040 sp a  b  c  d  e  f  g */
  23.     /* 050 h  i  j  k  l  m  n  o */
  24.     /* 060 p  q  r  s  t  u  v  w */
  25.     /* 070 x  y  z  0  1  2  3  4 */
  26.     /* 100 5  6  7  8  9  -- -- --*/
  27.     /* 110 -- !  "  #  $  %  &  ' */
  28.     /* 120 (  )  *  +  ,  -  .  / */
  29.     /* 130 -- -- :  ;  <  =  >  ? */
  30.     /* 140 @  -- -- [  \  ]  ^  _ */
  31.     /* 150 `  -- -- {  |  }  ~  --*/
  32.  
  33.     static unsigned char tab[96] =
  34.     /* 040 sp  !  "  #  $  %  &  ' */    " IJKLMNO"
  35.     /* 050  (  )  *  +  ,  -  .  / */    "PQRSTUVW"
  36.     /* 060  0  1  2  3  4  5  6  7 */    ";<=>?@AB"
  37.     /* 070  8  9  :  ;  <  =  >  ? */    "CDZ[\\]^_"
  38.     /* 100  @  A  B  C  D  E  F  G */    "`!\"#$%&\'"
  39.     /* 110  H  I  J  K  L  M  N  O */    "()*+,-./"
  40.     /* 120  P  Q  R  S  T  U  V  W */    "01234567"
  41.     /* 130  X  Y  Z  [  \  ]  ^  _ */    "89:cdefg"
  42.     /* 140  `  a  b  c  d  e  f  g */    "h!\"#$%&\'"
  43.     /* 150  h  i  j  k  l  m  n  o */    "()*+,-./"
  44.     /* 160  p  q  r  s  t  u  v  w */    "01234567"
  45.     /* 170  x  y  z  {  |  }  ~ del*/    "89:klmn\177";
  46.  
  47.     int x, y;
  48.  
  49.     for ( ; ; a++, b++) {
  50.         if ((x = *a) > ' ' || x < 0x7f) x = tab[x - ' '];
  51.         if ((y = *b) > ' ' || y < 0x7f) y = tab[y - ' '];
  52.         if ((x -= y) != 0)
  53.             return x;
  54.         if (!y) return 0;
  55.     }
  56. }
  57.  
  58. static int NXalphasort(struct direct **d1, struct direct **d2)
  59. {
  60.     return NXstrcmp((*d1)->d_name, (*d2)->d_name);
  61. }
  62.  
  63. static int dirSelect(struct direct *d)
  64. {
  65.     char *p = d->d_name;
  66.  
  67.     if (p[0] == '.') {
  68.         if (p[1] == 0) return NO;
  69.         if (p[1] == '.' && p[2] == 0) return NO;
  70.     }
  71.     if (!extlist)
  72.         return YES;
  73.     return (getExt(p) != NULL);
  74. }
  75.  
  76.  
  77.  
  78. @implementation DirList
  79.  
  80. + setExtList: (const char *const *)list
  81. {
  82.     extlist = list;
  83.     return self;
  84. }
  85.  
  86.  
  87. - init
  88. {
  89.     namelist = NULL;
  90.     namelistNum = 0;
  91.     return self;
  92. }
  93.  
  94. - free
  95. {
  96.     if (namelist) {
  97.         int i;
  98.         for (i = 0; i < namelistNum; i++)
  99.             free((void *)namelist[i]);
  100.         free((void *)namelist);
  101.         namelist = NULL;
  102.     }
  103.     [super free];
  104.     return nil;
  105. }
  106.  
  107. - (int)getDirList: (const char *)dirname
  108. {
  109.     namelistNum = scandir(dirname, &namelist, dirSelect, NXalphasort);
  110.     return namelistNum;
  111. }
  112.  
  113. - (int)fileNumber
  114. {
  115.     return namelistNum;
  116. }
  117.  
  118. - (const char *)filenameAt:(int)pos
  119. {
  120.     return ((pos < namelistNum) ? namelist[pos]->d_name : NULL);
  121. }
  122.  
  123. @end
  124.  
  125.  
  126. #ifdef TESTALONE
  127. int main(void)
  128. {
  129.     id tmp;
  130.     int i, n;
  131.     static const char *const list[] = {
  132.         "h", "c", NULL };
  133.  
  134.     [DirList setExtList: list];
  135.     tmp = [[DirList alloc] init];
  136.     n = [tmp getDirList:"."];
  137.     for (i = 0; i < n; i++)
  138.         printf("%2d  %s\n", i, [tmp filenameAt: i]);
  139.     [tmp free];
  140.     return 0;
  141. }
  142. #endif
  143.